// Do file examines the linearity of the reduced form and the first stage.
// Figures A2-A7.

// Last changed: 2020-09-28 (KOL)
  
  clear all
  set more off 
  set matsize 10000
  
  capture log close
  local output "C:\Userdata\Shared\Dofiles\DoAnalysis\PlacementCandidacy"
  
  use "E:\ProjData\UtplaceringsData8594\utp-UtpData.dta", clear 
  
  merge 1:1 LopNr Ar using "E:\ProjData\UtplaceringsData8594\utp-EthnicContacts.dta"
  
  replace ArbAnd = 0 if ArbAnd == .
  
  replace mNom = mNom*100
  replace mVald = mVald*100
  
  rename mNom Nominerad
  rename mVald Vald
  destring sKommun, gen(KommunValAr) 
  
//Keep only refugee immigrants
  keep if FlyktInv == 1
    
//Recode earnings to monthly wages in 1000 SEK
  replace LoneInk = ((LoneInk*100)/12)/1000

  gen Kvinna = -1*(Kon-1)
  
  local sample "inrange(InvAr, 1987, 1991) & inrange(InvAlder, 18, 55) & inrange(Ar, 1991, 2014) & FlyktInv == 1 & Rostratt == 1"
  local controls1 "InvAr IFAUkod Ar FodelseAr"
  local controls2 "Kvinna Gift_InvAr UtbAr_InvAr Barn0_15_InvAr"
  local controls3 "LoneInk UtbAr Sysselsatt" 
  
  capture drop lpx lpy lpse lCI hCI ehatY ehatX
  capture drop lpx_r lpy_r lpse_r
  
// Create local polynomial graphs for the reduced form effect
  lpoly Nominerad logpEnklavVuxenInvAr if `sample' , degree(2) n(100) name(g1, replace) ci recast(scatter) msize(vtiny)  gen(lpx_r lpy_r) se(lpse_r)
  gen lCI_r = lpy_r- 1.96*lpse_r
  gen hCI_r = lpy_r+ 1.96*lpse_r
  
  gen aY = 1.7*Nominerad/100
  replace aY = -.25 if Nominerad == 0
 
  twoway (scatter aY logpEnklavVuxenInvAr if inrange(logpEnklavVuxenInvAr, -9, -3), xscale(range(-9 -3)) xtitle("Ethnic Concentration in Year of Immigration") ytitle("Probability of Nomination (%)") jitter(3 3) mcolor(%5) msize(tiny))  ///
  (rarea lCI_r hCI_r lpx_r if inrange(lpx_r, -9, -3), xlabels(-9(1)-3) ylabels(0 .5 1 1.5) xscale(range(-9 -3)) yscale(range(0 1.8)) color(gs12))  ///
  (line lpy_r lpx_r if inrange(lpx_r, -9, -3),  yscale(range(0 1.8)) xscale(range(-9 -3)) lpattern(solid) lcolor(black)), legend(off) name(g1a, replace) graphregion(color(white)) 
 
 // Create a residualized version of this graph 
  reghdfe Nominerad `controls2' if `sample', absorb(InKommun `controls1') resid(ehatY_r)
  reghdfe logpEnklavVuxenInvAr `controls2' if `sample', absorb(InKommun `controls1') resid(ehatX_r)
  
  lpoly ehatY_r ehatX_r if `sample' , degree(2) n(100) name(g1b, replace) ci recast(scatter) msize(vtiny)  gen(lpx_rr lpy_rr) se(lpse_rr)
  gen lCI_rr = lpy_rr- 1.96*lpse_rr
  gen hCI_rr = lpy_rr+ 1.96*lpse_rr
  line lpy_rr lCI_rr hCI_rr lpx_rr if inrange(lpx_rr, -2, 1.2)
  
  replace aY = .42*Nominerad/100
  replace aY = -.2 if Nominerad == 0
  
  twoway (scatter aY ehatX_r if inrange(ehatX_r, -2, 1.2), xtitle("Residualized Ethnic Concentration at Year of Immigration") ytitle("Residualized Probability of Nomination (%)") jitter(3 3) mcolor(%5) msize(tiny))  ///
  (rarea lCI_rr hCI_rr lpx_rr if inrange(lpx_rr, -2, 1.2), xlabels(-2(.5)1.5) ylabels(-.1 0 .1 .2 .3) yscale(range(-.2 .45)) color(gs12)) ///
  (line lpy_rr lpx_rr if inrange(lpx_rr, -2, 1.2),  lpattern(solid) lcolor(black)), legend(off) name(g1b, replace) graphregion(color(white)) 
    
  graph combine g1a g1b,  graphregion(color(white)) xsize(8) ysize(4)
  graph export "C:\Userdata\Shared\Dofiles\DoAnalysis\PlacementCandidacy\Output\RFGraph_1.png", replace
  
// Reduced form graph by year 
  foreach ar of numlist 1991 1994 1998 2002 2006 2010 2014 {
  capture drop lpx_r lpy_r lpse_r lCI_r hCI_r aY
  
  lpoly Nominerad logpEnklavVuxenInvAr if `sample' & Ar == `ar', degree(2) n(100) name(g1, replace) ci recast(scatter) msize(vtiny)  gen(lpx_r lpy_r) se(lpse_r)
  gen lCI_r = lpy_r- 1.96*lpse_r
  gen hCI_r = lpy_r+ 1.96*lpse_r
  
  gen aY = 2.2*Nominerad/100
  replace aY = -.75 if Nominerad == 0
 
  twoway (scatter aY logpEnklavVuxenInvAr if inrange(logpEnklavVuxenInvAr, -9, -3), xscale(range(-9 -3)) xtitle("EC{sub:0}") ytitle("PNom") jitter(2 2) mcolor(%5) msize(tiny))  ///
  (rarea lCI_r hCI_r lpx_r if inrange(lpx_r, -9, -3), xlabels(-9(1)-3) ylabels(0 .75 1.5) xscale(range(-9 -3)) yscale(range(-.5 2.4)) color(gs12))  ///
  (line lpy_r lpx_r if inrange(lpx_r, -9, -3),  yscale(range(-.5 2.4)) xscale(range(-9 -3)) lpattern(solid) lcolor(black)), title("`ar'") legend(off) name(g`ar', replace) graphregion(color(white)) 
 

 } 
  graph combine g1991 g1994 g1998 g2002 g2006 g2010 g2014, graphregion(color(white))
  graph export "C:\Userdata\Shared\Dofiles\DoAnalysis\PlacementCandidacy\Output\RFGraph_2.png", replace
  
  // Reduced form graph by year, residualized versio
  foreach ar of numlist 1991 1994 1998 2002 2006 2010 2014 {
  capture drop ehatY_r ehatX_r lCI_rr hCI_rr aY lpx_rr lpy_rr lpse_rr
  
  reghdfe Nominerad `controls2' if `sample' & Ar == `ar' , absorb(InKommun `controls1') resid(ehatY_r)
  reghdfe logpEnklavVuxenInvAr `controls2' if `sample' & Ar == `ar', absorb(InKommun `controls1') resid(ehatX_r)
  nois sum ehatX_r, detail 
  
  lpoly ehatY_r ehatX_r if `sample', degree(2) n(100) name(g1b, replace) ci recast(scatter) msize(vtiny)  gen(lpx_rr lpy_rr) se(lpse_rr)
  gen lCI_rr = lpy_rr- 1.96*lpse_rr
  gen hCI_rr = lpy_rr+ 1.96*lpse_rr
  line lpy_rr lCI_rr hCI_rr lpx_rr if inrange(lpx_rr, -2, 1.2)
  
  gen aY = .
  replace aY = .75*Nominerad/100
  replace aY = -.5 if Nominerad == 0
  
  twoway (scatter aY ehatX_r if inrange(ehatX_r, -2, 1.2), xtitle("REC{sub:0}") ytitle("RPNom") jitter(1 1) mcolor(%5) msize(tiny))  ///
  (rarea lCI_rr hCI_rr lpx_rr if inrange(lpx_rr, -2, 1.2), xlabels(-2(.5)1.5) ylabels(-.3 .15 .6) yscale(range(-.3 .8)) color(gs12)) ///
  (line lpy_rr lpx_rr if inrange(lpx_rr, -2, 1.2),  lpattern(solid) lcolor(black)), title("`ar'") legend(off) name(g`ar', replace) graphregion(color(white)) 
 
 } 
  
  graph combine g1991 g1994 g1998 g2002 g2006 g2010 g2014, graphregion(color(white))
  graph export "C:\Userdata\Shared\Dofiles\DoAnalysis\PlacementCandidacy\Output\RFGraph_3.png", replace
  
  local sample "inrange(InvAr, 1987, 1991) & inrange(InvAlder, 18, 55) & inrange(Ar, 1991, 2014) & FlyktInv == 1 & Rostratt == 1"
  local controls1 "InvAr IFAUkod Ar FodelseAr"
  local controls2 "Kvinna Gift_InvAr UtbAr_InvAr Barn0_15_InvAr"
  local controls3 "LoneInk UtbAr Sysselsatt" 
  
// Create local polynomial graphs for the first stage, overall as well as by year  
  lpoly logpEnklavVuxen logpEnklavVuxenInvAr if `sample', degree(2) gen(lpx1 lpy1) se(lpse1) n(50) name(g2a, replace) ci recast(scatter) msize(vtiny)
  gen lCI1 = lpy1- 1.96*lpse1
  gen hCI1 = lpy1+ 1.96*lpse1
  
  twoway (scatter logpEnklavVuxen logpEnklavVuxenInvAr if inrange(logpEnklavVuxenInvAr, -9, -3), xtitle("Ethnic Concentration in Year of Immigration") ytitle("Ethnic Concentration in Year t") jitter(1 1) mcolor(%5) msize(vtiny))  ///
  (rarea lCI1 hCI1 lpx1 if inrange(lpx1, -9, -3), xlabels(-9(1)-3) xscale(range(-9 -3)) color(gs12)) ///
  (line lpy1 lpx1 if inrange(lpx1, -9, -3),  lpattern(solid) lcolor(black)), legend(off) name(g2a, replace) graphregion(color(white)) 

  reghdfe logpEnklavVuxen `controls2' if `sample', absorb(InKommun `controls1') resid(ehatY2)
  reghdfe logpEnklavVuxenInvAr `controls2' if `sample', absorb(InKommun `controls1') resid(ehatX2)
  
  lpoly ehatY2 ehatX2 if `sample' , degree(2) n(50) gen(lpx2 lpy2) se(lpse2) name(g2b, replace) ci recast(scatter) msize(vtiny) 
  gen lCI2 = lpy2- 1.96*lpse2
  gen hCI2 = lpy2+ 1.96*lpse2
  
  twoway (scatter ehatY2 ehatX2 if inrange(ehatX2, -2.2, 1.2), xtitle("Residualized Ethnic Concentration in Year of Immigration") ytitle("Residualized Ethnic Concentration in Year t")  jitter(1 1) mcolor(%5) msize(vtiny))  ///
  (rarea lCI2 hCI2 lpx2 if inrange(lpx2, -2.2, 1.2), xlabels(-2.2(.4)1.2) xscale(range(-2.2 1.2)) color(gs12)) ///
  (line lpy2 lpx2 if inrange(lpx2, -2.2, 1.2),  lpattern(solid) lcolor(black)), legend(off) name(g2b, replace) graphregion(color(white)) 

  graph combine g2a g2b,  graphregion(color(white)) xsize(8) ysize(4) 
  graph export "C:\Userdata\Shared\Dofiles\DoAnalysis\PlacementCandidacy\Output\FSGraph_1.png", replace
 
 local controls1 "InvAr IFAUkod FodelseAr"
 foreach ar of numlist 1991 1994 1998 2002 2006 2010 2014 {
  capture drop lpx1 lpy1 lpse1 lCI1 hCI1
  
  lpoly logpEnklavVuxen logpEnklavVuxenInvAr if `sample' & Ar == `ar', degree(2) gen(lpx1 lpy1) se(lpse1) n(50) name(g2a, replace) ci recast(scatter) msize(vtiny)
  gen lCI1 = lpy1- 1.96*lpse1
  gen hCI1 = lpy1+ 1.96*lpse1
  
  twoway (scatter logpEnklavVuxen logpEnklavVuxenInvAr if inrange(logpEnklavVuxenInvAr, -9, -3), xtitle("REC{sub:0}") ytitle("REC{sub:0}") jitter(1 1) mcolor(%40) msize(vtiny))  ///
  (rarea lCI1 hCI1 lpx1 if inrange(lpx1, -9, -3), xlabels(-9(1)-3) xscale(range(-9 -3)) color(gs12)) ///
  (line lpy1 lpx1 if inrange(lpx1, -9, -3),  lpattern(solid) lcolor(black)), title("`ar'") legend(off) name(g`ar', replace) graphregion(color(white)) 

 } 
  
  graph combine g1991 g1994 g1998 g2002 g2006 g2010 g2014, graphregion(color(white))
  graph export "C:\Userdata\Shared\Dofiles\DoAnalysis\PlacementCandidacy\Output\FSGraph_2.png", replace
 
 
 local controls1 "InvAr IFAUkod FodelseAr"
 foreach ar of numlist 1991 1994 1998 2002 2006 2010 2014 {
  capture drop lpx2 lpy2 ehatY2 ehatX2 lpse2 lCI2 hCI2
  
  reghdfe logpEnklavVuxen `controls2' if `sample' & Ar == `ar', absorb(InKommun `controls1') resid(ehatY2)
  reghdfe logpEnklavVuxenInvAr `controls2' if `sample' & Ar == `ar', absorb(InKommun `controls1') resid(ehatX2)
 
  lpoly ehatY2 ehatX2 if `sample' & Ar == `ar' , degree(2) n(50) gen(lpx2 lpy2) se(lpse2) ci recast(scatter) msize(vtiny) nograph
  gen lCI2 = lpy2- 1.96*lpse2
  gen hCI2 = lpy2+ 1.96*lpse2
 
  twoway (scatter ehatY2 ehatX2 if inrange(ehatX2, -2.2, 1.2) & inrange(ehatY2, -2.2, 1.2) , jitter(1 1) title("`ar'") ylabels(-2.2(1.2)1.2) xlabels(-2.2(.8)1.2) yscale(range(-2.2 1.2)) xtitle("REC{sub:0}") ytitle(REC{sub:t}) mcolor(%40) msize(vtiny))  ///
  (rarea lCI2 hCI2 lpx2 if inrange(lpx2, -2.2, 1.2) & inrange(lpy2, -2.2, 1.2), ylabels(-2.2(1.2)1.2) xlabels(-2.2(.8)1.2) xscale(range(-2.2 1.2)) yscale(range(-2.2 1.2)) color(gs12)) ///
  (line lpy2 lpx2 if inrange(lpx2, -2.2, 1.2) & inrange(lpy2, -2.2, 1.2), ylabels(-2.2(1.2)1.2) yscale(range(-2.2 1.2)) lpattern(solid) lcolor(black)), legend(off) name(g`ar', replace) graphregion(color(white)) ///
  

 } 
  
  graph combine g1991 g1994 g1998 g2002 g2006 g2010 g2014, graphregion(color(white))
  graph export "C:\Userdata\Shared\Dofiles\DoAnalysis\PlacementCandidacy\Output\FSGraph_3.png", replace
  
